package com.cn.sl.server.api.system.role.model.dto

import com.cn.sl.server.api.system.menu.model.entity.MenuEntity
import com.cn.sl.server.api.system.role.model.entity.RoleEntity
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull

@Schema(description = "编辑角色参数")
data class EditRoleParam(
    @field:NotNull(message = "用户id不能为空")
    var id: Long?,
    @field:NotBlank(message = "角色名称不能为空")
    @field:Schema(description = "角色名称")
    var name: String?,
    var memo: String?,
    var menuIds: List<Long>?
) {
    fun toDto(): EditRoleDto {
        return EditRoleDto(
            id = id!!,
            name = name!!,
            memo = memo,
            menuIds = menuIds
        )
    }
}

data class EditRoleDto(
    val id: Long,
    val name: String,
    val memo: String?,
    val menuIds: List<Long>?
) {
    fun merge(entity: RoleEntity, findMenus: (menuIds: List<Long>) -> List<MenuEntity>): RoleEntity {
        val dto = this
        return entity.apply {
            this.name = dto.name
            this.memo = dto.memo
            this.menus = dto.menuIds?.let { findMenus(it) } ?: listOf()
        }
    }
}